package com.ljd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljd.entity.CrmBanner;
import com.ljd.service.CrmBannerService;
import com.ljd.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理员首页banner表 前端控制器
 * </p>
 *
 * @author ljd
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/educms/bannerAdmin")
//@CrossOrigin
public class BannerAdminController {
    @Autowired
    CrmBannerService bannerService;
    @ApiOperation(value = "分页幻灯片列表")
    @GetMapping("getBanners/{current}/{pageSize}")
    public R getBanners(@PathVariable Integer current, @PathVariable Integer pageSize){
        Page<CrmBanner> bannerPage = new Page<>();
        bannerPage.setCurrent(current);
        bannerPage.setSize(pageSize);
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");

        IPage<CrmBanner> page = bannerService.page(bannerPage, wrapper);
        return R.ok().data("items",page.getRecords()).data("total",page.getTotal());
    }

    @ApiOperation(value = "ID查询幻灯片")
    @GetMapping("getBanner/{bannerId}")
    public R getBanner(@PathVariable String bannerId){
        CrmBanner banner = bannerService.getById(bannerId);
        return R.ok().data("banner",banner);
    }
    @ApiOperation(value = "添加幻灯片")
    @PostMapping("save")
    public R save(@RequestBody CrmBanner banner){
        boolean save = bannerService.save(banner);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @ApiOperation(value = "修改幻灯片")
    @PostMapping("update")
    public R update(@RequestBody CrmBanner banner){
        boolean b = bannerService.updateById(banner);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }

    }
    @ApiOperation(value = "删除幻灯片")
    @DeleteMapping("delete/{bannerId}")
    public R delete(@PathVariable String bannerId){
        boolean b = bannerService.removeById(bannerId);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }

}

